package com.kukudexin;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;

/**
 * @author: 酷酷的鑫
 * @Date: 2022/2/25 9:53
 * @description:
 */
public class SchedulerTest02 {

    public static void main(String[] args) {
        //延迟2s之后执行一个任务
        ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(10);
        scheduledExecutorService.schedule(() -> {
            System.out.println(Thread.currentThread().getId() + " --- " + System.currentTimeMillis());
        }, 2, TimeUnit.SECONDS);

//        scheduledExecutorService.scheduleAtFixedRate(()->{
//            System.out.println(Thread.currentThread().getId() + "----在固定频率开启任务---" + System.currentTimeMillis());
////            try {
////                TimeUnit.SECONDS.sleep(3);  //睡眠模拟任务执行时间 ,如果任务执行时长超过了时间间隔,则任务完成后立即开启下个任务
////            } catch (InterruptedException e) {
////                e.printStackTrace();
////            }
//        },3,2,TimeUnit.SECONDS);

        scheduledExecutorService.scheduleWithFixedDelay(() -> {
            System.out.println(Thread.currentThread().getId() + "----在固定频率开启任务---" + System.currentTimeMillis());
            try {
                TimeUnit.SECONDS.sleep(3);  //睡眠模拟任务执行时间 ,如果任务执行时长超过了时间间隔,则任务完成后立即开启下个任务
                System.out.println("任务结束");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }, 3, 2, TimeUnit.SECONDS);
    }
}
